<?php
/*
 * 作用： 这里面封装了数据库的相关操作,和一些全局的操作
 * 
 */
define("URL" ,"http://192.168.0.102/MicrowoWeb/login.html");
define("DB_IP", "localhost"); //数据库的ip
define("PORT", 3306); //数据库端口
define("USER_NAME", "user01"); //
define("USER_PASSWORD", "123456");
define("DB_NAME", "microlive");


//用于连接默认的数据库
//返回值： 失败返回0 ，成功返回sql 连接
function ConnectDB()
{
    $con = mysql_connect(DB_IP, USER_NAME, USER_PASSWORD, DB_NAME);
    if (!$con) {
        return 0;
    }
    mysql_select_db(DB_NAME, $con);
    return $con;
}


//
function QueryDB($sqlCommand, $con)
{
    $result = mysql_query($sqlCommand, $con);
    if (!$result) {
        return 0;
    }
    
    return $result;
}


function GetChangeRow()
{
    return mysql_affected_rows();
}


function CloseConnect($con)
{
    mysql_close($con);
}


function FreeSqlResult($result)
{
    if ($result != null)
        mysql_free_result($result);
    
}

//从sql的结果集上面获取一行结果
//参数 resultDB： QueryDB执行成功后的返回值
//返回值 ：失败反回NULL ，成功返回 数据库表的一行数据
function GetResult($resultDB)
{
    $row = mysql_fetch_row($resultDB);
    return $row;
}

function GetArrayInresult($result)
{
    return mysql_fetch_array($result, MYSQL_ASSOC);
}

function GetNumber($resultDB)
{
    $row = mysql_fetch_array($resultDB);
    return $row;
}

//更新表
//返回值： 成功 -- true  失败 -- false
function UpdateDB($sqlCommand)
{
    if (!QueryDB($sqlCommand)) {
        return false;
    }
    return true;
}

//执行插入命令，对表出入数据，成功返回 ID , 失败返回0
function InsertTable($sqlCommand, $con)
{
    if (!mysql_query($sqlCommand, $con)) {
        return 0;
    }
    $laseInsertId = "select LAST_INSERT_ID()";
    $result       = mysql_query($laseInsertId, $con);
    if ($result == NULL) {
        return 0;
    }
    $row = mysql_fetch_row($result);
    return $row[0];
}

//返回结果行数
function GetResultRow($row)
{
    return mysql_num_rows($row);
}

function getResultOject($sqlCommand, $con, $queryErrorCode)
{
    $result = mysql_query($sqlCommand, $con);
    if (!$result) {
        echo json_encode(array(
            'Result' => "-6"
        ));
        die();
    }
    
    $count = mysql_num_rows($result);
    
    if (0 == $count) {
        echo json_encode(array(
            'Result' => $queryErrorCode
        ));
        die();
    }
    return $result;
}



// 以下是会话的相关操作
function SessionIsExist($sessionId) //会话存在并且没过时
{
    return true;
}

function verifyUser($sessionId, $userId)
{
	$con = mysql_connect(DB_IP, USER_NAME, USER_PASSWORD, DB_NAME);
    if (!$con) {
        return false;
    }
    mysql_select_db(DB_NAME, $con);
    $sqlCommand = "SELECT * FROM mw_web_session WHERE wsSessionID='$sessionId'";
    $result     = mysql_query($sqlCommand, $con);
    if (!$result) {
        echo json_encode(array(
            'Result' => "-6"
        ));
        die();
    }
    if (0 == mysql_num_rows($result)) {
        return false;
    }
	$row = mysql_fetch_row($result);
    if (0 != strcmp($row[2], $userId)) {
        return false;
    }
    
    global $userType;
    $userType = $row[4];
    global $typeId;
    $typeId = $row[3];
    return true;
}

function InsertSessionId($userId, $con)
{
    $sqlCommand = "SELECT wsSessionID FROM ws_web_session WHERE wsUserID='$userId'";
    $result     = mysql_query($sqlCommand, $con);
    if (!$result) {
        echo json_encode(array(
            'Result' => "-6"
        ));
        die();
    }
    $sessionId;
    if (0 == GetResultRow($result)) {
        $sqlCommand = "INSERT INTO ws_web_session(wsUserID) VALUES('$userId')";
        $sessionId  = InsertTable($sqlCommand, $con);
    } else {
        $row       = mysql_fetch_row($result);
        $sessionId = $row[0];
    }
    return $sessionId;
}

function insertManagerSession($userId, $con, $userType, $typeID)
{
	$sqlCommand = "DELETE FROM mw_web_session WHERE wsUserID='$userId'";
	$result     = mysql_query($sqlCommand, $con);
    if (!$result) {
        echo json_encode(array(
            'Result' => "-6"
        ));
        die();
    }
	$sqlCommand = "INSERT INTO mw_web_session(wsUserID,wsUserType,wsTypeID) VALUES('$userId','$userType','$typeID')";
    $sessionId  = InsertTable($sqlCommand, $con);
    return $sessionId;
}

function QuitSession()
{
}

//多条语句要求同时插入的时候，插了一半出现错误，将这些错误和执行过程记录下来
function RecordTheError($content)
{
    $file = fopen("/error.log", "a+");
    if (!$file); {
        echo "open file failure";
        echo $content;
        return false;
    }
    $res = fwrite($file, $content);
    if (!$res) {
        echo "write to file failure";
        echo $content;
        return false;
    }
    fclose($file);
    return true;
}


?>













